﻿using System;
using System.Collections.Generic;
using System.Text;
using Buka.BLL.Model;
using Buka.DAL;

namespace Buka.BLL.Provider
{
    [System.ComponentModel.DataObject]
    public class UserProvider
    {
        Buka.DAL.UsersTableAdapters.UsersTableAdapter _userTableAdapter = new Buka.DAL.UsersTableAdapters.UsersTableAdapter();

        #region "DB methods"

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
        public bool ValidateUser(string username, string password)
        {
            return false;
        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public long CreateUser(User user)
        {
            try
            {
                Nullable<long> userId;
                userId = 0;
                _userTableAdapter.User_Create(user.UserName,User.EncryptPassword(user.Password,user.UserName), user.Email, user.FirstName,
                    user.LastName, user.City, user.Ranking, user.Sex, user.UserTypeId, user.StatusId, user.CreatedDate,
                    user.LastLoginDate, user.IsOnline, user.BirthDate, user.Avatar, user.PersonalMssage, ref userId);
                return userId.Value;
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }


        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
        public User GetUserByUsername(string username)
        {
            List<User> users = new List<User>();
            foreach (Buka.DAL.Users.UsersRow row in _userTableAdapter.GetUserByUsername(username))
            {
                User user = GetObjectFromDataRow(row);
                users.Add(user);
            }
            if (users.Count > 1)
            {
                throw new ArgumentException("Postoje više od 2 usera sa istim username-om!");
            }
            else if (users.Count == 0)
            {
                return null;
            }
            else
            {
                return users[0];
            }

        }

        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, false)]
        public User GetByCredentials(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentNullException("username", "Username must be set");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password", "Password must be set. To get user only by username use GetByUsername(string) method.");
            }
            //string passwordCipher = User.DecryptPassword(password, username);
            string passwordCipher = User.EncryptPassword(password, username);
            //passwordCipher = passwordCipher.Substring(0, 10);
            //OwisSecurity.uUserDataTable udt = new OwisSecurity.uUserDataTable();
            Buka.DAL.Users.UsersDataTable udt = new Users.UsersDataTable();
            Buka.DAL.UsersTableAdapters.UsersTableAdapter usersTableAdapter = new Buka.DAL.UsersTableAdapters.UsersTableAdapter();
            //Buka.DAL.Users.tempDataTable tdt = new Users.tempDataTable();
            //Buka.DAL.UsersTableAdapters.tempTableAdapter tableAdapter = new Buka.DAL.UsersTableAdapters.tempTableAdapter();
            try
            {
                //tdt = tableAdapter.GetData(username, passwordCipher);
                udt = usersTableAdapter.GetDataByCredentials(username, passwordCipher);
            }
            catch (Exception ex)
            {
                
                //throw new ArgumentNullException( ex.Message);
            }
            

            if (udt.Count == 1)
            {
                return GetObjectFromDataRow(udt[0]);
            }
            else
            {
                return null;
            }
        }


        #endregion


        #region "Fill & Get"

        public void FillObjectFromDataRow(Buka.DAL.Users.UsersRow row, User user)
        {
            user.Id = row.id;
            user.UserName = row.username;
            user.Password = row.password;
            user.Email = row.email;
            user.FirstName = row.first_name;
            user.LastName = row.last_name;
            user.City = row.city;
            user.Ranking = row.ranking;
            user.Sex = row.sex;
            user.UserTypeId = row.user_type_id;
            user.StatusId = row.status_id;
            user.CreatedDate = row.created_date;
            if (row.Islast_login_dateNull())
            {
                user.LastLoginDate = null;
            }
            else
            {
                user.LastLoginDate = row.last_login_date;
            }
            user.IsOnline = row.is_online;
            user.BirthDate = row.birth_date;
            if (row.IsavatarNull())
            {
                user.Avatar = null;
            }
            else
            {
                user.Avatar = row.avatar;
            }
            if (row.Ispersonal_messageNull())
            {
                user.PersonalMssage = null;
            }
            else
            {
                user.PersonalMssage = row.personal_message;
            }
        }

        public User GetObjectFromDataRow(Buka.DAL.Users.UsersRow row)
        {
            User user = new User();
            try
            {
                FillObjectFromDataRow(row, user);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            return user;
        }

        #endregion
    }
}
